﻿using System;
using System.IO;

namespace DatabaseStuff.Basic
{
    public class FileSource: TextSource
    {
        private string folderpath;

        public FileSource()
            :base()
        {
            folderpath = String.Empty;
        }

        public FileSource(string _folderpath)
            :base()
        {
            InitializeFolderpath(_folderpath);
        }

        internal FileSource(TextStorageFormatSettings settings)
            : base(settings)
        {
            folderpath = String.Empty;
        }

        internal FileSource(TextStorageFormatSettings settings, string _folderpath)
            :base(settings)
        {
            InitializeFolderpath(_folderpath);
        }

        private void InitializeFolderpath(string _folderpath)
        {
            folderpath = _folderpath;
            if (!folderpath.EndsWith("\\"))
                folderpath += "\\";
        }

        public string FolderPath
        {
            get { return folderpath; }
            set { folderpath = value; }
        }

        protected override TextReader GetSchemaReader(string table_name)
        {
            TextReader result = new StreamReader(File.Open(folderpath + table_name + ".schema", FileMode.Open));
            return result;
        }

        protected override TextReader GetRecordsReader(string table_name)
        {
            TextReader result = new StreamReader(File.Open(folderpath + table_name + ".table", FileMode.Open));
            return result;
        }

        protected override TextReader GetTableNamesReader()
        {
            TextReader result = new StreamReader(File.Open(folderpath + "tables.dat", FileMode.Open));
            return result;
        }
    }
}
